package com.kye.express.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.widget.Toast;

import com.kye.express.utils.ActivityManager;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class ToastManager {

    private static Toast toast = null;

    public static void show(Context context, @StringRes int resId) {
        show(context, resId, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, @StringRes int resId, @Duration int duration) {
        Toast.makeText(context, resId, duration).show();
    }

    public static void show(Context context, CharSequence text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    public static void show(Context context, CharSequence text, int duration) {
        Toast.makeText(context, text, duration).show();
    }

    /**
     * 居中显示且自动判断线程的吐司（短时间显示）
     *
     * @param text author  SkyYu
     */
    public static void showCenterForShort(@NonNull final CharSequence text) {
        final Activity currentActivity = ActivityManager.getInstance().getCurrentActivity();
        if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
            if (toast == null) {
                toast = Toast.makeText(currentActivity, text, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            } else {
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            }
            toast.show();
        } else {
            (currentActivity).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (toast == null) {
                        toast = Toast.makeText(currentActivity, text, Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    } else {
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    }
                    toast.show();
                }
            });
        }

    }

    /**
     * 居中显示且自动判断线程的吐司（长时间显示）
     *
     * @param text author  SkyYu
     */
    public static void showCenterForLong(@NonNull final CharSequence text) {
        final Activity currentActivity = ActivityManager.getInstance().getCurrentActivity();
        if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
            if (toast == null) {
                toast = Toast.makeText(currentActivity, text, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            } else {
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            }
            toast.show();
        } else {
            (currentActivity).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (toast == null) {
                        toast = Toast.makeText(currentActivity, text, Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    } else {
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    }
                    toast.show();
                }
            });
        }
    }

    /**
     * 居中显示且自动判断线程的吐司
     *
     * @param text
     * @param duration author  SkyYu
     */
    public static void showCenter(@NonNull final CharSequence text, @Duration final int duration) {
        final Activity currentActivity = ActivityManager.getInstance().getCurrentActivity();
        if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
            if (toast == null) {
                toast = Toast.makeText(currentActivity, text, duration);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            } else {
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setText(text);
            }
            toast.show();
        } else {
            (currentActivity).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (toast == null) {
                        toast = Toast.makeText(currentActivity, text, duration);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    } else {
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.setText(text);
                    }
                    toast.show();
                }
            });
        }
    }

    public static void dismiss() {
        if (toast != null) {
            toast.cancel();
        }
    }

    @IntDef({Toast.LENGTH_SHORT, Toast.LENGTH_LONG})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Duration {
    }
}
